<template>
  <div class="app-container">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item name="1">
        <template slot="title">
          <i class="el-icon-search" />搜索栏
        </template>
        <div class="head-container">
          <el-form v-if="show" class="searchform" :model="params" :inline="true" size="mini">
            <el-form-item label="DEPT+FINElINE">
              <el-input v-model="params.deptFineline" placeholder="输入对应列名搜索" clearable size="mini" style="width: 200px;" />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" style="float:right;margin-right:0px;" icon="el-icon-refresh" round @click="resetForm">重置</el-button>
              <el-button size="mini" type="primary" style="float:right;margin-right:0px;" icon="el-icon-search" round @click="toQuery(params.deptFineline)">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div style="padding-top:5px">
      <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;" icon="el-icon-remove" round @click="batchDelete">批量删除</el-button>
      <el-upload
        ref="upload"
        :auto-upload="false"
        :on-change="handleOnChange"
        :show-file-list="false"
        :limit="1"
        :multiple="false"
        action="#"
        accept=""
        style="float:right;margin-bottom:5px;margin-left:0px;"
      >
        <el-button size="mini" type="primary" icon="el-icon-upload2" round>批量导入Excel</el-button>
      </el-upload>
      <el-button size="mini" type="primary" style="float:right;margin-left:0px;" icon="el-icon-download" round @click="doDownloadTemplate">下载模板</el-button>
      <el-button v-if="hasRole(['R017001'])" size="mini" type="success" style="float:right;margin-bottom:5px;margin-right:0px;" round icon="el-icon-circle-plus" @click="addInfo">新增</el-button>
      <el-table :data="data" :row-key="getRowKeys" stripe size="small" border style="width: 100%;" @selection-change="handleSelectionChange">
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" label="DEPT+FINElINE" prop="deptFineline" />
        <el-table-column :show-overflow-tooltip="true" label="DEPT" prop="dept" />
        <el-table-column :show-overflow-tooltip="true" label="FINELINE" prop="fineline" />
        <el-table-column :show-overflow-tooltip="true" label="FINELINE DESC" prop="finelineDesc" />
        <el-table-column :show-overflow-tooltip="true" label="Type2" prop="type2" />
        <el-table-column :show-overflow-tooltip="true" label="SYSREPL" prop="sysRepl" />
        <el-table-column :show-overflow-tooltip="true" label="创建人" prop="createdBy" />
        <el-table-column :show-overflow-tooltip="true" label="最后更新人" prop="updatedBy" />
        <el-table-column :show-overflow-tooltip="true" label="创建时间" prop="createdTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="最后更新时间" prop="updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="185" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" icon="el-icon-edit" round @click="updateInfoOne(scope.row)">修改</el-button>
            <el-button v-if="hasRole(['R017001'])" size="mini" type="warning" icon="el-icon-remove" round @click="deleteInfoOne(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <addOrUpdateInfo ref="addOrUpdateInfo" :parent="parent" />
  </div>
</template>

<script>
import { batchDeleteFreshFoodPiFineline, batchFreshFoodPiFinelineExcel, getDownLoadTemplateUrl } from '@/api/plugs'
import initServiceData from '@/utils/initServiceData'
import addOrUpdateInfo from '@/views/system-manage/plugs/freshFoodPiFineline/addOrUpdateInfo'
import { parseTime } from '@/utils/index'
import { Message } from 'element-ui'
import { authentication } from '@/utils/processingData.js'

export default {
  components: { addOrUpdateInfo },
  mixins: [initServiceData],
  data() {
    return {
      parent: this,
      activeName: '1',
      ruleApplyTypes: [{ name: '', value: null }],
      options: [],
      ids: [],
      show: true
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
    // this.loadApplyType()
    // this.loadSheetNames()
  },
  methods: {
    parseTime,
    beforeInit() {
      this.url = '/plugs/queryFreshFoodPiFinelineList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    resetForm() {
      this.params = { rowSite: '', rowName: '', options: [] }
    },
    toQuery(rep) {
      this.page = 0
      this.currentPage = 1
      if (!rep) {
        this.init()
        return
      }
      var numReg = /^[0-9]*$/
      var numRe = new RegExp(numReg)
      if (!numRe.test(rep)) {
        Message({
          message: '请输入数字 ',
          type: 'warning',
          duration: 2 * 1000,
          showClose: true
        })
      } else {
        this.init()
      }
    },
    addInfo() {
      const _this = this.$refs.addOrUpdateInfo
      _this.dataInfo = {}
      _this.dialog = true
      _this.dialogStatus = 'addMap'
    },
    updateInfoOne(row) {
      const _this = this.$refs.addOrUpdateInfo
      _this.dialog = true
      _this.dialogStatus = 'editMap'
      _this.dataInfo = Object.assign({}, row)
    },
    // 翻页选择row的key值
    getRowKeys(row) {
      return row.mainId
    },
    // 列表多选框触发
    handleSelectionChange(val) {
      this.ids = []
      val.forEach(item => {
        this.ids.push(item.id)
      })
    },
    // 批量删除
    batchDelete() {
      if (this.ids.length <= 0) {
        Message({
          message: '请选择删除项!',
          type: 'warning',
          duration: 2 * 1000
        })
        return
      }
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteFreshFoodPiFineline(this.ids).then(res => {
          Message({
            message: '操作成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.toQuery()
        })
      })
    },
    deleteInfoOne(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteFreshFoodPiFineline([row.id]).then(res => {
          Message({
            message: '操作成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.toQuery()
        })
      })
    },
    toItemInfo(item) {
      console.log('sss:' + item.createdBy)
      const _this = this.$refs.itemForm
      _this.itemInfo = item // JSON.parse(
      _this.dialog = true
    },
    // 模板下载
    doDownloadTemplate() {
      // var uri = process.env.VUE_APP_BASE_API2 + '/plugs/downloadTypeTemplate' + '?changeType=7&templateType=4'
      var val = '?changeType=201&templateType=1'
      getDownLoadTemplateUrl(val).then(res => {
        if (res) {
          window.open(authentication(res))
        } else {
          Message({
            message: '未找到模板请模板维护',
            type: 'warning',
            duration: 2 * 1000
          })
        }
      })
    },
    // 文件变动的钩子
    handleOnChange(file, fileList) {
      let cpfRole = this.hasRole(['R017013'])
      if (this.hasRole(['R017001']) === true) {
        cpfRole = false
      }
      const ex = file.raw.name.substring(file.raw.name.lastIndexOf('.') + 1)
      if (ex !== 'xlsx') {
        Message({
          message: '请上传格式为 xlsx 的Excel文件。',
          type: 'error',
          duration: 3 * 1000
        })
        fileList.pop()
      } else {
        this.loading = true
        var formData = new FormData()
        formData.append('file', file.raw)
        formData.append('cpfRole', cpfRole)
        batchFreshFoodPiFinelineExcel(formData).then(res => {
          this.toQuery()
          Message({
            message: '操作成功，上传数据行数：' + res,
            type: 'success',
            duration: 3 * 1000
          })
          this.loading = false
        }, error => {
          console.log(error)
          this.loading = false
        })
        fileList.pop()
      }
    }
  }
}
</script>
